/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        if(sorter.order && sorter.field) {
            // 点击排序
            option.sortField = sorter.field;
            if(sorter.order === 'descend') option.isDesc = true;
            else option.isDesc = false;
        } else {
            // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};
export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);

import {formatMessage} from './intl';

export const validateAdd = data => {
    const {id, dealerId, details = []} = data;
    const msgs = [];
    if(!id && !dealerId)
        msgs.push(
            formatMessage({
                id: 'utils.dealerId',
                defaultMessage: '总代'
            })
        );
    if(!details.length)
        msgs.push(
            formatMessage({
                id: 'utils.details',
                defaultMessage: '清单'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};

export const validateUpdate = data => {
    const {containerNo, cabinetType} = data;
    const msgs = [];
    if(!containerNo)
        msgs.push(
            formatMessage({
                id: 'utils.containerNo',
                defaultMessage: '柜图号'
            })
        );
    if(!cabinetType)
        msgs.push(
            formatMessage({
                id: 'utils.cabinetType',
                defaultMessage: '柜型'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};

export const PAGINATION_OPTIONS = Object.freeze({
    showSizeChanger: true,
    showQuickJumper: true,
    pageSizeOptions: Object.freeze(['100']),
    showTotal: total => formatMessage({
        id: 'table.showTableTotal',
        defaultMessage: '共 {total} 条记录'
    }, {total})
});
